package com.yb.mini.common.common;

import lombok.Data;
import lombok.Getter;

/**
 * 接口返回数据统一封装类
 *
 * @author yangbiao
 * @date 2023/12/15
 */
@Data
public class Result<T> {
    private int code;
    private String message;
    private T data;

    /**
     * 成功方法
     *
     * @param t
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(T t) {
        final Result result = new Result();
        result.setCode(ResultEnum.OK.code);
        result.setMessage(ResultEnum.OK.message);
        result.setData(t);
        return result;
    }

    /**
     * 失败方法
     *
     * @param code
     * @param message
     * @param <T>
     * @return
     */
    public static <T> Result<T> error(int code, String message) {
        final Result result = new Result();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    /**
     * 响应枚举(自定义响应信息码扩展)
     *
     * @author yangbiao
     * @date 2023/12/15
     */
    @Getter
    public enum ResultEnum {
        //成功
        OK(2000, "OK"),
        //自定义异常
        CUSTOM_EXCEPTION(4000, "网络异常"),
        //抛给用户的异常提示
        CUSTOM_OPEN_EXCEPTION(40000, "");

        private final int code;
        private final String message;

        ResultEnum(int code, String message) {
            this.code = code;
            this.message = message;
        }
    }

}
